package com.coyote.common.base;


import com.github.pagehelper.PageInfo;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

public class PageUtils<T> implements Serializable {

    private static final long serialVersionUID = 1L;
    private int total;
    private List<T> data;

    public PageUtils() {
        this.total = 0;
        this.data = Collections.emptyList();
    }

    public PageUtils(int total, List<T> data) {
        this.total = total;
        if (data == null) {
            this.data = Collections.emptyList();
        } else {
            this.data = data;
        }
    }

    public static <T> PageUtils<T> convertPageInfo(PageInfo<T> page) {
        if (page == null) {
            throw new IllegalArgumentException("PageInfo对象不能为null");
        }

        int total = (int) page.getTotal();
        List<T> data = page.getList();
        if (data == null) {
            data = Collections.emptyList();
        }

        return new PageUtils<>(total, data);
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        if (data == null) {
            this.data = Collections.emptyList();
        } else {
            this.data = data;
        }
    }
}

